﻿using LeanApp.Model.Assist;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LeanApp.Bll
{
    /// <summary>
    /// 质量分析业务
    /// </summary>
    public class QualityBll
    {
        /// <summary>
        /// 所有样本数据
        /// </summary>
        public List<QualitySample> AllItem
        {
            get
            {
                return _allItem;
            }
        }
        /// <summary>
        /// 设置数据
        /// </summary>
        /// <param name="data">数据</param>
        public void SetData(List<Dictionary<string, object>> data)
        {
            if (data == null || data.Count <= 0)
            {
                return;
            }

            foreach (Dictionary<string, object> item in data)
            {
                int valQty = item.Keys.Count();
                if (valQty < 3)
                {
                    continue;
                }
                List<string> names = item.Keys.ToList();
                QualitySample sample = new QualitySample();
                for (int i = 0; i < names.Count; i++)
                {
                    if (i == 0)
                    {
                        sample.Id = Convert.ToInt32(item[names[i]]);
                        SetColMapper("Id", names[i]);
                    }
                    else if (i == 1)
                    {
                        sample.QualityReason = Convert.ToString(item[names[i]]);
                        SetColMapper("QualityReason", names[i]);
                    }
                    else if (i == 2)
                    {
                        sample.QualityTimes = Convert.ToInt32(item[names[i]]);
                        SetColMapper("QualityTimes", names[i]);
                    }
                }
                QualitySample existSample = _allItem.Find(b => b.QualityReason == sample.QualityReason);
                if (existSample == null)
                {
                    _allItem.Add(sample);
                }
                else
                {
                    existSample.QualityTimes = existSample.QualityTimes + sample.QualityTimes;
                }
            }
            _allItem = _allItem.OrderByDescending(a => a.QualityTimes).ToList();
        }

        /// <summary>
        /// 直方图数据
        /// </summary>
        public Dictionary<string, int> RectData
        {
            get
            {
                Dictionary<string, int> res = new Dictionary<string, int>();
                if (_allItem == null || _allItem.Count <= 0)
                {
                    return res;
                }
                _allItem.ForEach(a =>
                {
                    res[a.QualityReason] = a.QualityTimes;
                });
                return res;
            }
        }

        /// <summary>
        /// 累积百分比曲线数据
        /// </summary>
        public Dictionary<string, double> LineData
        {
            get
            {
                Dictionary<string, double> res = new Dictionary<string, double>();
                if (_allItem == null || _allItem.Count <= 0)
                {
                    return res;
                }
                int totalTime = _allItem.Select(a => a.QualityTimes).Sum();
                for (int i = 0; i < _allItem.Count; i++)
                {
                    int sumTime = 0;
                    for (int j = 0; j <= i; j++)
                    {
                        sumTime = sumTime + _allItem[j].QualityTimes;
                    }
                    res[_allItem[i].QualityReason] = Math.Round(100.0 * sumTime / totalTime, 2);
                }
                return res;
            }
        }

        /// <summary>
        /// 获取列名映射
        /// </summary>
        /// <returns>列名映射</returns>
        public Dictionary<string, string> GetColMapper()
        {
            return _colMapper;
        }

        /// <summary>
        /// 设置列名映射
        /// </summary>
        /// <param name="name">列名</param>
        /// <param name="val">映射名</param>
        private void SetColMapper(string name, string val)
        {
            _colMapper[name] = val;
        }

        /// <summary>
        /// 列名映射
        /// </summary>
        private Dictionary<string, string> _colMapper = new Dictionary<string, string>();

        /// <summary>
        /// 样本数据
        /// </summary>
        private List<QualitySample> _allItem = new List<QualitySample>();
    }
}
